package com.ps;

/**
 * @author: Jie Wang
 * @date: 2021/3/5 15:34
 * 选择排序:每轮循环找到 最小值的位置 然后与第一个位置的值进行交换，循环的轮数为数组的长度-1
 */
public class SingletonSort {

    public static void main(String[] args) {
        int[] arr = {6, 3, 5, 1, 9, 2, 4, 7, 8};
        for (int j = 0; j < arr.length - 1; j++) {
            int minPos = j;
            for (int i = j + 1; i < arr.length; i++) {
                if (arr[i] < arr[minPos]) minPos = i;

            }
            System.out.println("minPos:" + minPos);
            swap(arr,minPos,j);
            print(arr);
        }
    }

    private static void swap(int [] arr , int i,int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    private static void print(int [] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
